/**
 * 
 */
package com.jdy.haoduoaiteacher.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.jdy.haoduoaiteacher.model.CategoryItem;
import com.jdy.haoduoaiteacher.model.CustomHabitItem;
import com.jdy.haoduoaiteacher.model.RadioCategoryItem;
import com.jdy.haoduoaiteacher.util.Tools;
import com.squareup.picasso.Picasso;
import com.ycsj.goldmedalnewconcept.R;

import java.util.List;

/**
 * 课程课件
 */
public class CustomHabitGridViewAdapter extends BaseAdapter {

  private final Context mContext;

  private final List<CustomHabitItem> mRadioCategoryItemList;

  public CustomHabitGridViewAdapter(Context ctx, List<CustomHabitItem> list) {
    this.mContext = ctx;
    this.mRadioCategoryItemList = list;
  }

  /*
   * (non-Javadoc)
   * 
   * @see android.widget.Adapter#getCount()
   */
  @Override
  public int getCount() {
    if (this.mRadioCategoryItemList != null)
      return this.mRadioCategoryItemList.size();
    else
      return 0;
  }

  /*
   * (non-Javadoc)
   * 
   * @see android.widget.Adapter#getItem(int)
   */
  @Override
  public Object getItem(int position) {
    if (this.mRadioCategoryItemList != null)
      return this.mRadioCategoryItemList.get(position);
    else
      return null;
  }

  /*
   * (non-Javadoc)
   * 
   * @see android.widget.Adapter#getItemId(int)
   */
  @Override
  public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
  }

  /*
   * (non-Javadoc)
   * 
   * @see android.widget.Adapter#getView(int, android.view.View,
   * android.view.ViewGroup)
   */
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    final CustomHabitItem rci = this.mRadioCategoryItemList.get(position);
    if (convertView == null) {
      convertView = LayoutInflater.from(mContext).inflate(
          R.layout.item_grid_text, parent, false);
      holder = new ViewHolder();
      holder.root_view = convertView.findViewById(R.id.root_view);
      holder.title = (TextView) convertView.findViewById(R.id.title);
      holder.imgCover = (ImageView) convertView.findViewById(R.id.cover);
      holder.circleImg = (ImageView) convertView.findViewById(R.id.img);
      // holder.circleImg.setVisibility(View.INVISIBLE);
      convertView.setTag(holder);
    } else {
      holder = (ViewHolder) convertView.getTag();
    }


    if (Tools.isNotEmpty(rci.icon)) {
        Picasso
                .with(mContext)
                .load(rci.icon)
                .placeholder(
                        mContext.getResources().getDrawable(R.drawable.default_cover))
                .error(mContext.getResources().getDrawable(R.drawable.default_cover))
                .into(holder.imgCover);
    }


    holder.title.setText(rci.name);

    return convertView;
  }

  private CategoryItem convert2CI(RadioCategoryItem rci) {
    // TODO Auto-generated method stub
    CategoryItem ci = new CategoryItem();
    ci.id = rci.id;
    ci.name = rci.name;
    ci.code = rci.code;
    if (rci.enabled)
      ci.enabled = 1;
    else
      ci.enabled = 0;
    ci.sort = rci.sort;
    ci.created_at = rci.created_at;
    ci.updated_at = rci.updated_at;
    ci.deleted_at = rci.deleted_at;
    ci.icon = rci.icon;
    ci.catalog = rci.catalog;
    ci.home_page = rci.home_page;
    return ci;
  }

  private class ViewHolder {
    private TextView title;
    private ImageView imgCover;
    private ImageView circleImg;
    private View root_view;
  }

}
